﻿using System;
using System.Threading;
using Oscilloscope.Adjust;
using Oscilloscope.Exception;

namespace Oscilloscope.Screen
{
	// Token: 0x0200002D RID: 45
	internal class VoscRefreshTimer
	{
		// Token: 0x0600014D RID: 333 RVA: 0x00009CD6 File Offset: 0x00007ED6
		public VoscRefreshTimer(IOscilloscopeScreen oscs)
		{
			this.oscs = oscs;
			this.isStarted = false;
			this.isRequestingStop = false;
		}

		// Token: 0x0600014E RID: 334 RVA: 0x00009CF7 File Offset: 0x00007EF7
		public void Start()
		{
			if (!this.IsStoped())
			{
				return;
			}
			ThreadPool.QueueUserWorkItem(new WaitCallback(this.worker), null);
		}

		// Token: 0x0600014F RID: 335 RVA: 0x00009D18 File Offset: 0x00007F18
		private void worker(object param)
		{
			try
			{
				this.isStarted = true;
				long num = 40L;
				long num2 = 1000L;
				long num3 = 0L;
				for (;;)
				{
					Thread.Sleep(15);
					try
					{
						if (!this.isRequestingStop)
						{
							double num4 = TimePerCellAdjustGrade.Instance.GetValueOfGrade(this.oscs.GetTimePerCellGrade()) * 3.0 * 1000.0;
							double num5;
							if (num4 >= (double)num2)
							{
								num5 = (double)num2;
							}
							else if (num4 <= (double)num)
							{
								num5 = (double)num;
							}
							else
							{
								num5 = num4;
							}
							if ((double)(DateTime.Now.Ticks / 10000L - num3) > num5)
							{
								this.oscs.RefreshData();
								num3 = DateTime.Now.Ticks / 10000L;
							}
							continue;
						}
						this.isRequestingStop = false;
					}
					catch (System.Exception ex)
					{
						Console.WriteLine("VoscRefreshTimer error:" + ex.Message);
						continue;
					}
					break;
				}
			}
			catch (System.Exception ex2)
			{
				Console.WriteLine("VoscRefreshTimer error:" + ex2.Message);
			}
			finally
			{
				this.isStarted = false;
			}
		}

		// Token: 0x06000150 RID: 336 RVA: 0x00009E7C File Offset: 0x0000807C
		public void Stop()
		{
			if (this.IsStoped())
			{
				return;
			}
			this.isRequestingStop = true;
		}

		// Token: 0x06000151 RID: 337 RVA: 0x00009E90 File Offset: 0x00008090
		public bool IsStoped()
		{
			return !this.isStarted;
		}

		// Token: 0x040000A7 RID: 167
		private IOscilloscopeScreen oscs;

		// Token: 0x040000A8 RID: 168
		private volatile bool isStarted;

		// Token: 0x040000A9 RID: 169
		private volatile bool isRequestingStop;
	}
}
